package com.cardmanager.pro.business.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import com.cardmanager.pro.pojo.ConsumeTaskPOJO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cardmanager.pro.business.CreditCardAccountBusiness;
import com.cardmanager.pro.business.CreditCardAccountHistoryBusiness;
import com.cardmanager.pro.pojo.CreditCardAccount;
import com.cardmanager.pro.repository.CreditCardAccountRepository;
@Service
public class CreditCardAccountBusinessImpl implements CreditCardAccountBusiness {

	@Autowired
	private CreditCardAccountRepository creditCardAccountRepository;
	
	@Autowired
	private CreditCardAccountHistoryBusiness creditCardAccountHistoryBusiness;
	
	@Autowired
	private EntityManager em;


	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 历史查询账单日
	 * @param userId
	 * @param creditCardNumber
	 * @return
	 */
	@Override
	public CreditCardAccount findByUserIdAndCreditCardNumber(String userId, String creditCardNumber) {
		return creditCardAccountRepository.findByUserIdAndCreditCardNumber(userId, creditCardNumber);
	}

	/**
	 * 历史查询账单日
	 * @param userId
	 * @param creditCardNumber
	 * @return
	 */
	public CreditCardAccount queryCreditCardAccount (String userId, String creditCardNumber){
		String sql = "select * from t_credit_card_account where user_id = '" + userId + "' and credit_card_number = '" + creditCardNumber + "' ";
		SqlRowSet set = jdbcTemplate.queryForRowSet(sql);
		CreditCardAccount account = new CreditCardAccount();
		while (set.next()){

			account.setCreditCardNumber(set.getString("credit_card_number"));
			account.setBlance(set.getBigDecimal("blance"));
			account.setCreditBlance(set.getBigDecimal("credit_blance"));
			account.setPhone(set.getString("phone"));
			account.setBillDate(set.getInt("bill_date"));
			account.setRepaymentDate(set.getInt("repayment_date"));
		}
		return account;
	}

	/**
	 * 保存账单日
	 * @param creditCardAccount
	 * @return
	 */
	@Transactional
	@Override
	public CreditCardAccount save(CreditCardAccount creditCardAccount) {
		creditCardAccount = creditCardAccountRepository.save(creditCardAccount);
		em.flush();
		em.clear();
		return creditCardAccount;
	}


	/**
	 * 保存账单日和历史记录表
	 * @param userId
	 * @param creditCardNumber
	 * @param taskId
	 * @param addOrSub
	 * @param amount
	 * @param description
	 * @return
	 */
	@Override
	@Transactional
	public CreditCardAccount updateCreditCardAccount(String userId,String creditCardNumber,String taskId,int addOrSub,BigDecimal amount,String description){
		CreditCardAccount creditCardAccount = creditCardAccountRepository.findByUserIdAndCreditCardNumber(userId, creditCardNumber);
		if(creditCardAccount != null){
			//判断任务执行状态
			if(0 == addOrSub){
				creditCardAccount.setBlance(creditCardAccount.getBlance().add(amount).setScale(2, BigDecimal.ROUND_HALF_UP));
				creditCardAccount.setLastUpdateTime(new Date());
				//保存账单日
				creditCardAccount = creditCardAccountRepository.save(creditCardAccount);
				//保存历史记录表
				creditCardAccountHistoryBusiness.createNewHistory(addOrSub,amount,taskId,creditCardAccount.getId(),creditCardAccount.getBlance(),description);
			}else if(1 == addOrSub){
				creditCardAccount.setBlance(creditCardAccount.getBlance().subtract(amount).setScale(2, BigDecimal.ROUND_HALF_UP));
				creditCardAccount.setLastUpdateTime(new Date());
				creditCardAccount = creditCardAccountRepository.save(creditCardAccount);
				creditCardAccountHistoryBusiness.createNewHistory(addOrSub,amount,taskId,creditCardAccount.getId(),creditCardAccount.getBlance(),description);
			}
		}
		return creditCardAccount;
	}

	/**
	 * 查询历史记录表
	 * @return
	 */
	@Override
	public List<CreditCardAccount> findCreditCardAccountByBlanceNotZero() {
		return creditCardAccountRepository.findCreditCardAccountByBlanceNotZero();
	}
	
}
